package statistics;

import java.util.Random;

/**
 * This class represents a Normal Distribution.
 */

public class NormalDistribution extends ContinuousDistribution {

  private double mu;
  private double sigma;

  /**
   * Constructs a Normal Distribution with parameters mu and sigma. You must also
   * specify the random seed that will be used to realize random variables with this
   * distribution.
   * @param mu the expectation.
   * @param sigma the standard deviation.
   */

  public NormalDistribution(double mu, double sigma) {
    this(mu, sigma, new Random());
  }

  /**
   * Constructs a Normal Distribution with parameters mu and sigma. You must also
   * specify the random seed that will be used to realize random variables with this
   * distribution.
   * @param mu the expectation.
   * @param sigma the standard deviation.
   * @param random the random number generator
   */

  public NormalDistribution(double mu, double sigma, Random random) {
    this.mu = mu;
    this.sigma = sigma;
    this.random = random;
  }

  /**
   * Returns the expectation of a Normal distribution.
   */

  public double expectation() {
    return mu;
  }

  /**
   * Returns the variance of a normal distribution.
   */

  public double variance() {
    return sigma*sigma;
  }

  /**
   * Returns the densiti function of a normal distribution in x
   * @param x
   */
   
  public double densityFunction(double x) {
    return Math.exp(-(x-mu)*(x-mu)/2.0/sigma/sigma)/Math.sqrt(2*Math.PI)/sigma;
  }

  /**
   * Returns P(X <= z), X is a random variable with Normal distribution.
   * @param z
   */

  public double probabilityFunction(double z) {
    double norm = Statistics.standardNormal(100, (z-mu)/sigma);
    return norm;
  }

  /**
   * Returns a realization of a random variable with a Normal distribution.
   */

  public double nextRandom() {
    return random.nextGaussian()*sigma+mu;
  }
}
